package com.hongxun.design.jungong.utils;

import android.text.TextUtils;

import com.hongxun.design.MyApp;
import com.hongxun.design.jungong.model.MaterialDetail;
import com.hongxun.design.sqlite.bean.OldBj;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * Created by cqcqwu on 2017/8/4.
 */

public class jxlUtil {

    public static List<OldBj> readBj2DB(File f){
        List<OldBj> list=new ArrayList<>();
        try {
            Workbook course=Workbook.getWorkbook(f);
            Sheet sheet = course.getSheet(0);
            for(int i=1;i<sheet.getRows();i++){
                OldBj oldBj=new OldBj();
                String s1=toStringV(sheet.getCell(0,i).getContents());
                String s2=toStringV(sheet.getCell(1,i).getContents());
                String s3=toStringV(sheet.getCell(2,i).getContents());
                String s4=toStringV(sheet.getCell(3,i).getContents());
                String s5=toStringV(sheet.getCell(4,i).getContents());
                String s6=toStringV(sheet.getCell(5,i).getContents());
                String s7=toStringV(sheet.getCell(6,i).getContents());
                oldBj.setCode(s1);
                oldBj.setName(s2);
                oldBj.setAddress(s3);
                oldBj.setType(s4);
                oldBj.setBxCode(s5);
                oldBj.setBjCode(s6);
                oldBj.setFactoryCode(s7);
                list.add(oldBj);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return list;
    }

    //读取Excel表
    public static List<MaterialDetail> read2DB(File f) {
        ArrayList<MaterialDetail> billList = new ArrayList<>();
        try {
            Workbook course = Workbook.getWorkbook(f);
            //Sheet的下标是从0开始,获取第一张Sheet表
            Sheet sheet = course.getSheet(0);
            //获取Sheet表中所包含的总行数
            for (int i = 1; i < sheet.getRows(); i++) {
                MaterialDetail detail = new MaterialDetail();
                String s1 = toStringV(sheet.getCell(0, i).getContents());
                String s2 = toStringV(sheet.getCell(1, i).getContents());
                String s3 = toStringV(sheet.getCell(2, i).getContents());
                String s4 = toStringV(sheet.getCell(3, i).getContents());
                String s5 = toStringV(sheet.getCell(4, i).getContents());
                String s6 = toStringV(sheet.getCell(5, i).getContents());
//                if (!s1.trim().isEmpty() && !s2.trim().isEmpty()) {
                detail.setMaterial_number(s1);
                detail.setType(s2);
                detail.setName(s3);//！=null
                detail.setSpec(s4);
//                detail.setNum(s4.isEmpty() ? "" : s4);//！=null
                detail.setNum(s5);//！=null
                detail.setUnit(s6);
                billList.add(detail);
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return billList;
    }

    //把String转化为double
    public static double convertToDouble(String number, double defaultValue) {
        if (TextUtils.isEmpty(number)) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(number);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    private static String toStringV(Object obj) {
        return String.valueOf(obj);
    }

    public static Object getValueByRef(Class cls, String fieldName) {
        Object value = null;
        fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName
                .substring(0, 1).toUpperCase());
        String getMethodName = "get" + fieldName;
        try {
            Method method = cls.getMethod(getMethodName);
            value = method.invoke(cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
